package name.wilbur.course.db.bigone.ui;

import java.util.Iterator;
import java.util.Map;

import name.wilbur.course.db.bigone.control.PersonalInfoMgr;
import name.wilbur.course.db.bigone.model.Article;
import name.wilbur.course.db.bigone.model.BaseDBModel;
import name.wilbur.course.db.bigone.model.City;
import name.wilbur.course.db.bigone.model.Profile;
import name.wilbur.course.db.bigone.model.User;
import name.wilbur.course.db.bigone.ui.custom.ListContentProvider;
import name.wilbur.course.db.bigone.ui.custom.ListLabelProvider;
import name.wilbur.course.db.bigone.ui.custom.ListModel;

import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ListViewer;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

public class UserInfoWin extends BaseWindow {
	private boolean editable;
	
	private static String nameText = "姓名:  ";
	private static String emailText = "邮件:  ";
	private static String registerText = "注册时间: ";
	private static String genderText = "性别: ";
	private static String phoneText = "电话: ";
	private static String cityText = "城市: ";
	
	private PersonalInfoMgr mgr;
	private ListViewer logList;
	private Group logGroup, infoGroup;
	private Label infoNameL, infoGenderL, infoPhoneL, infoCityL, infoEmailL, infoRegisterDateL;
	
	private Article currArticle;
	private ListModel<BaseDBModel> logModel;
	
	public UserInfoWin(User friend, Map<String, Object> session) {
		super();
		mgr = new PersonalInfoMgr(friend);
		mgr.setSession(session);
		if (mgr.getMe() != mgr.getUser()) {
			editable = false;
		} else {
			editable = true;
		}
	}

	public UserInfoWin(Display disp, User friend, Map<String, Object> session) {
		super(disp);
		mgr = new PersonalInfoMgr(friend);
		mgr.setSession(session);
	}
	@Override
	public void createSShell() {
		final GridData oneSpanGridData = new GridData(GridData.CENTER, GridData.FILL, true, false, 1, 1);
		final GridData twoSpanGridData = new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1);
		GridData gridData;
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 5;
		gridLayout.makeColumnsEqualWidth = true;
		
		sShell = new Shell();
		sShell.setText("用户信息");
		sShell.setLayout(gridLayout);
		sShell.setSize(new Point(500, 300));
//		sShell.setBackgroundImage(new Image(null, "images/x1.jpg"));
		
		gridLayout = new GridLayout();
		gridLayout.numColumns = 2;
		// info friend group
		infoGroup = new Group(sShell, SWT.BORDER);
		infoGroup.setText("用户信息");
		infoGroup.setLayout(gridLayout);
		infoGroup.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, false, 2, 6));
		// info name label
		infoNameL = new Label(infoGroup, SWT.None);
		infoNameL.setLayoutData(twoSpanGridData);
		// info gender label
		infoGenderL = new Label(infoGroup, SWT.None);
		infoGenderL.setLayoutData(twoSpanGridData);
		// info phone label
		infoPhoneL = new Label(infoGroup, SWT.None);
		infoPhoneL.setLayoutData(twoSpanGridData);
		// info city label
		infoCityL = new Label(infoGroup, SWT.None);
		infoCityL.setLayoutData(twoSpanGridData);
		// info email label
		infoEmailL = new Label(infoGroup, SWT.None);
		infoEmailL.setLayoutData(twoSpanGridData);
		// info register date label
		infoRegisterDateL = new Label(infoGroup, SWT.None);
		infoRegisterDateL.setLayoutData(twoSpanGridData);
		
		gridLayout = new GridLayout();
		gridLayout.numColumns = 3;
		// log view group
		logGroup = new Group(sShell, SWT.None);
		logGroup.setText("用户日志");
		logGroup.setLayout(gridLayout);
		logGroup.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, true, 3, 6));
		// log list
		logList = new ListViewer(logGroup, SWT.V_SCROLL);
		logList.setContentProvider(new ListContentProvider<User>(logList));
		logList.setLabelProvider(new ListLabelProvider());
		logList.getList().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 3, 5));
		logList.addSelectionChangedListener(new ISelectionChangedListener() {
			public void selectionChanged(SelectionChangedEvent event) {
				IStructuredSelection selection = (IStructuredSelection) event.getSelection();
				Iterator it = selection.iterator();
				if (it.hasNext())
					currArticle = (Article)it.next();
//				System.out.println(currArticle);
			}
		});
		// view log button
		Button viewLogB = new Button(logGroup, SWT.None);
		viewLogB.setLayoutData(oneSpanGridData);
		viewLogB.setText("查看日志");
		viewLogB.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
			public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
				ArticleWindow aw = new ArticleWindow(disp, currArticle, logModel, mgr.getSession(), editable);
				aw.open();
			}
		});
	}
	
	public void initWindowData() {
		logModel = new ListModel<BaseDBModel>(mgr.getArticles());
		logList.setInput(logModel);
		
		Profile p = mgr.getProfile();
		City c = mgr.getCity();
		if (p != null) {
			genderText += p.getGender();
			phoneText += p.getPhoneNumber();
		}
		if (c != null)
			cityText += c.getCityName();
		
		infoGenderL.setText(genderText);
		infoPhoneL.setText(phoneText);
		infoNameL.setText(nameText + mgr.getUser().getName());
		infoEmailL.setText(emailText + mgr.getUser().getEmail());
		infoRegisterDateL.setText(registerText + mgr.getUser().getRegisterDate());
		infoCityL.setText(cityText);
		
	}
}
